package com.henry.chatkit.input

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.henry.chatkit.R
import com.henry.chatkit.utils.appContext


class MoreItemAdapter(var data: List<InputMoreActionUnit>) : RecyclerView.Adapter<MoreItemAdapter.RecyclerViewHolder>() {

    val count = data.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        return RecyclerViewHolder(
            LayoutInflater.from(parent.context)
                .inflate(R.layout.chat_input_layout_action, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {

        val item = data[position]

        if (item.iconResId > 0) {
            holder.imageView.setImageResource(item.iconResId)
        }
        if (item.titleId > 0) {
            holder.textView.text = appContext().getString(item.titleId)
        }
        if (item.title.isNotEmpty()) {
            holder.textView.text = item.title
        }

        holder.contentView.setOnClickListener {
            item.onClickListener?.onClick(it)
        }

    }

    override fun getItemCount(): Int {
        return try {
            count
        } catch (e: Exception) {
            0
        }
    }

    class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val contentView: LinearLayout by lazy { itemView.findViewById(R.id.contentView) }
        val imageView: ImageView by lazy { itemView.findViewById(R.id.imageView) }
        val textView: TextView by lazy { itemView.findViewById(R.id.textView) }
    }


}